package com.ssh.filter;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
@WebFilter(urlPatterns= {"/view/*"})
public class SessionFilter implements Filter {

	public void destroy() {
	}

	public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException, ServletException {
		HttpServletRequest request = (HttpServletRequest) req;
		HttpSession session=request.getSession();
		
		String path = request.getServletPath();
		if(path.endsWith("InitServlet.do")|| path.endsWith("LoginServlet.do") || path.endsWith("RegisterServlet.do")){
			chain.doFilter(req, resp);
		}else {
			if(null!=session.getAttribute("name")) {
				chain.doFilter(req, resp);
			}else {
				PrintWriter out=resp.getWriter();
				resp.setContentType("text/html;charset=UTF-8");
				out.print("<script>parent.window.location.href='"+request.getServletContext().getContextPath()+"/InitServlet.do'"+"</script>");
				out.close();
			}
		}
		
		
		
	}

	public void init(FilterConfig fConfig) throws ServletException {
	}

}
